package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.dto.ChatGroupAssociationDTO;
import com.zenithmind.chat.pojo.vo.ChatGroupAssociationVO;
import com.zenithmind.chat.service.ChatGroupAssociationService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 群组关联控制器 - 遵循单一职责原则
 * 只负责群组关联的HTTP接口处理
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/chat/group-associations")
@RequiredArgsConstructor
@Validated
@Tag(name = "群组关联管理", description = "群组关联相关接口")
public class ChatGroupAssociationController {

    private final ChatGroupAssociationService chatGroupAssociationService;

    @PostMapping
    @Operation(summary = "创建群组关联", description = "创建新的群组关联")
    public Result<String> createAssociation(@Valid @RequestBody ChatGroupAssociationDTO dto) {
        String userId = UserContext.getUserId();
        String associationId = chatGroupAssociationService.createAssociation(dto, userId);
        return Result.success(associationId);
    }

    @DeleteMapping("/{associationId}")
    @Operation(summary = "删除群组关联", description = "删除指定的群组关联")
    public Result<Boolean> deleteAssociation(
            @Parameter(description = "关联ID") @PathVariable @NotBlank String associationId) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupAssociationService.deleteAssociation(associationId, userId);
        return Result.success(result);
    }

    @GetMapping("/group/{groupId}")
    @Operation(summary = "查询群组关联列表", description = "根据群组ID查询关联列表")
    public Result<List<ChatGroupAssociationVO>> getAssociationsByGroupId(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String userId = UserContext.getUserId();
        List<ChatGroupAssociationVO> associations = chatGroupAssociationService.getAssociationsByGroupId(groupId, userId);
        return Result.success(associations);
    }

    @GetMapping("/type/{associationType}")
    @Operation(summary = "查询类型关联列表", description = "根据关联类型查询关联列表")
    public Result<List<ChatGroupAssociationVO>> getAssociationsByType(
            @Parameter(description = "关联类型") @PathVariable @NotBlank String associationType) {
        String userId = UserContext.getUserId();
        List<ChatGroupAssociationVO> associations = chatGroupAssociationService.getAssociationsByType(associationType, userId);
        return Result.success(associations);
    }

    @GetMapping("/association/{associationId}")
    @Operation(summary = "查询关联的群组列表", description = "根据关联ID查询关联的群组列表")
    public Result<List<ChatGroupAssociationVO>> getAssociationsByAssociationId(
            @Parameter(description = "关联ID") @PathVariable @NotBlank String associationId) {
        String userId = UserContext.getUserId();
        List<ChatGroupAssociationVO> associations = chatGroupAssociationService.getAssociationsByAssociationId(associationId, userId);
        return Result.success(associations);
    }

    @GetMapping("/check")
    @Operation(summary = "检查关联是否存在", description = "检查指定的群组关联是否存在")
    public Result<Boolean> checkAssociationExists(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "关联类型") @RequestParam @NotBlank String associationType,
            @Parameter(description = "关联ID") @RequestParam @NotBlank String associationId) {
        Boolean exists = chatGroupAssociationService.checkAssociationExists(groupId, associationType, associationId);
        return Result.success(exists);
    }

    @PostMapping("/batch")
    @Operation(summary = "批量创建群组关联", description = "批量创建多个群组关联")
    public Result<Boolean> batchCreateAssociations(@Valid @RequestBody @NotEmpty List<ChatGroupAssociationDTO> dtoList) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupAssociationService.batchCreateAssociations(dtoList, userId);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除群组关联", description = "批量删除多个群组关联")
    public Result<Boolean> batchDeleteAssociations(@RequestBody @NotEmpty List<String> associationIds) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupAssociationService.batchDeleteAssociations(associationIds, userId);
        return Result.success(result);
    }
}
